﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.EmphasizeContinuousMfg.CApECMEmphasizeContinuousMfgCmd
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System.Resources;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.EmphasizeContinuousMfg;

public class CApECMEmphasizeContinuousMfgCmd : TxToggleButtonCommand
{
  private bool m_pressed;
  private CApECMEmphasizeContinuousMfgEnabler _enabler;
  private CUiECMEmphasizeContinuousMfgDlg m_emphasizeDialog;
  private CApECMEmphasizingModel m_model;
  private CApECMEmphasizingManager m_manager;

  public CApECMEmphasizeContinuousMfgCmd()
  {
    this._enabler = new CApECMEmphasizeContinuousMfgEnabler();
  }

  public virtual bool Connect() => true;

  public virtual bool Licensed => TxCommandLicenser.EmphasizeContinuousMFGLicense;

  public virtual string Bitmap
  {
    get => this.GetResourceManager().GetString("EMPHASIZE_CONTINUOUS_MFG_BITMAP");
  }

  public virtual string Category
  {
    get => this.GetResourceManager().GetString("EMPHASIZE_CONTINUOUS_MFG_CATEGORY");
  }

  public virtual string Description
  {
    get => this.GetResourceManager().GetString("EMPHASIZE_CONTINUOUS_MFG_DESCRIPTION");
  }

  public virtual string Name
  {
    get => this.GetResourceManager().GetString("EMPHASIZE_CONTINUOUS_MFG_NAME");
  }

  public virtual string Tooltip
  {
    get => this.GetResourceManager().GetString("EMPHASIZE_CONTINUOUS_MFG_TOOLTIP");
  }

  private ResourceManager GetResourceManager()
  {
    return new ResourceManager("DnProcessSimulateCommands.EmphasizeContinuousMfg.ECMStringTable", ((object) this).GetType().Assembly);
  }

  public virtual ITxCommandEnabler CommandEnabler => (ITxCommandEnabler) this._enabler;

  public virtual ITxImage Image
  {
    get => (ITxImage) TxImageManager.GetImageByKey("EmphasizeContinuousMfg");
  }

  public virtual void Execute(object cmdParams)
  {
    if (!this._enabler.Enable)
      return;
    if (this.Pressed)
    {
      this.Pressed = false;
      this.TurnOffMode();
    }
    else
    {
      this.Pressed = true;
      this.TurnOnMode();
    }
  }

  private void TurnOnMode()
  {
    TxApplication.ActiveDocument.Unloading += new TxDocument_UnloadingEventHandler(this.OnUnloadDocument);
    this.InitializeSingletons();
    if (this.m_manager != null)
    {
      this.m_manager.SetWidthToAllMfgs(false);
      this.m_manager.EmphasizeAllMfgsColor(false);
      TxApplication.RefreshDisplay();
    }
    this.m_emphasizeDialog = new CUiECMEmphasizeContinuousMfgDlg();
    ((Control) this.m_emphasizeDialog).Show();
  }

  private void InitializeSingletons()
  {
    this.m_model = CApECMEmphasizingModel.Initialize(CApECMEmphasizingManager.GetWidthFromOptions(), CApECMEmphasizingManager.GetColorFromOptions(), CApECMEmphasizingManager.GetConsiderColorFromOptions());
    this.m_manager = CApECMEmphasizingManager.Initialize();
  }

  private void TurnOffMode()
  {
    TxApplication.ActiveDocument.Unloading -= new TxDocument_UnloadingEventHandler(this.OnUnloadDocument);
    if (this.m_emphasizeDialog != null)
    {
      this.m_emphasizeDialog.CloseDlg();
      this.m_emphasizeDialog = (CUiECMEmphasizeContinuousMfgDlg) null;
    }
    this.m_manager.UnEmphasizeAllMfgs();
    this.DisposeSingletons();
    this.m_model = (CApECMEmphasizingModel) null;
    this.m_manager = (CApECMEmphasizingManager) null;
  }

  private void DisposeSingletons()
  {
    CApECMEmphasizingManager.DisposeSingleton();
    CApECMEmphasizingModel.DisposeSingleton();
  }

  private void OnUnloadDocument(object sender, TxDocument_UnloadingEventArgs args)
  {
    this.Pressed = false;
    this.TurnOffMode();
  }

  public virtual bool Push => this.Pressed;

  public bool Pressed
  {
    get => this.m_pressed;
    set => this.m_pressed = value;
  }
}
